package 系统复习.链表;

import datastructure.ListNode;

/**
 * 给你两个单链表的头节点 headA 和 headB ，请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点，返回 null 。
 */

public class L0207 {

    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {

        if (headA == null || headB == null) return null;

        ListNode first = headA;
        ListNode second = headB;

        int indexA = 1, indexB = 0;

        while (first != null || second != null) {
            if (first == null) {
                if (indexA == 1) {
                    first = headB;
                    indexA = 0;
                } else {
                    first = headA;
                    indexA = 1;
                }
            }
            if (second == null) {
                if (indexB == 1) {
                    second = headB;
                    indexB = 0;
                } else {
                    second = headA;
                    indexB = 1;
                }
            }

            if (first == second) {
                return first;
            }
            first = first.next;
            second = second.next;
        }

        return null;
    }


    public static void main(String[] args) {

    }

}
